<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>小米商城</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            background: url('img/敦敦.jpg') no-repeat center center fixed;
            background-size: cover;
        }
        .login-container {
            width: 320px;
            margin: 32px auto 0 auto;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            padding: 28px 20px;
        }
        .login-container h2 { text-align: center; margin-bottom: 24px; }
        .form-group { margin-bottom: 18px; }
        .form-group label { display: block; margin-bottom: 6px; color: #333; }
        .form-group input {
            width: 100%; padding: 8px 12px; border: 1px solid #ccc; border-radius: 4px;
            font-size: 16px;
        }
        .login-btn {
            width: 100%; padding: 10px; background: #007bff; color: #fff;
            border: none; border-radius: 4px; font-size: 16px; cursor: pointer;
            transition: background 0.2s;
        }
        .login-btn:hover { background: #0056b3; }
        .error { color: #e74c3c; text-align: center; margin-bottom: 10px; }
        .success { color: #27ae60; text-align: center; margin-bottom: 10px; }
    </style>
</head>
<body>
<div class="login-container">
    <h2>用户登录</h2>
    <div id="msg"></div>
    <form id="loginForm" onsubmit="return false;">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required autofocus>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
        <button class="login-btn" onclick="login()">登录</button>
    </form>
</div>
<script>
function login() {
    var username = document.getElementById('username').value.trim();
    var password = document.getElementById('password').value.trim();
    var msg = document.getElementById('msg');
    msg.innerHTML = '';
    if (!username || !password) {
        msg.innerHTML = '<div class="error">请输入用户名和密码</div>';
        return;
    }
    fetch('/user/login', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: 'username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password)
    })
    .then(response => response.json())
    .then(data => {
        console.log(data);
        // 兼容多种后端返回格式
        var token = data.token || (data.data && data.data.token) || data.data;
        if (token) {
            msg.innerHTML = '<div class="success">登录成功，正在跳转...';
            localStorage.setItem('token', token);
            setTimeout(function() {
                window.location.href = 'http://localhost:9012/success.html';
            }, 2000);
        } else {
            msg.innerHTML = '<div class="error">登录失败</div>';
        }
    })
    .catch(() => {
        msg.innerHTML = '<div class="error">请求失败，请重试</div>';
    });
}
</script>
</body>
</html> 